#include <osg/ShapeDrawable>
#include <osg/Geometry>
#include <osg/Geode>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>

#include <drawtool.h>

// y = a * x * x
osg::Geode* createParabola(double a = 1.0, double minx = -5.0, double maxx = 5.0, int segn = 10)
{
    if (segn <= 0)
    {
        return nullptr;
    }

    std::vector<osg::Vec3> pts;
    double dx = (maxx - minx) / segn;
    for (int i = 0; i < segn; i++)
    {
        double x = minx + dx * i;
        double y = a * x * x;
        pts.push_back( osg::Vec3(x, y, 0.f));
    }
    {
        double x = maxx;
        double y = a * x * x;
        pts.push_back(osg::Vec3(x, y, 0.f));
    }
    
    return drawtool::createPolyLine(pts, osg::Vec4(1, 1, 1, 1));
}

int main(int argc, char** argv)
{
    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild(createParabola(0.15,-5.0,5.0,100));
    root->addChild(createParabola(0.25, -5.0, 5.0, 100));
    root->addChild(createParabola(0.35, -5.0, 5.0, 100));
    root->addChild(createParabola(1.05, -5.0, 5.0, 100));

    osgViewer::Viewer viewer;

    viewer.setUpViewInWindow(50, 50, 800, 600);
    osgGA::TrackballManipulator* trackball = new osgGA::TrackballManipulator;
    trackball->setHomePosition(osg::Vec3(0, 0, 100), osg::Vec3(0, 0, 0), osg::Vec3(0, 1, 0), false);
    viewer.setSceneData(root.get());
    viewer.setCameraManipulator(trackball);
    return viewer.run();
}